package com.ybxx.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * 功能：文件操作辅助类
 * 公司：XA.Co.LTD
 * 制作人：Tom.Y
 * 日期：2011-05*/
public class FileUtil{
	private static Log logger = LogFactory.getLog(FileUtil.class);
	
	public static String generateFilename(String originalFilename){
		SimpleDateFormat dirSdf = new SimpleDateFormat("yyyyMM");
		String filePre = dirSdf.format(new Date());

		String fileExt = "";
		int lastIndex = originalFilename.lastIndexOf('.');

		if (lastIndex != -1) {
			fileExt = originalFilename.substring(lastIndex);
		}
		String filename = filePre + "/" + UUIDGenerator.getUUID() + fileExt;
		return filename;
	}
	
	public static void writeFile(String filePath, String data){
		FileOutputStream fos = null;
		OutputStreamWriter writer = null;
		try {
			fos = new FileOutputStream(new File(filePath));
			writer = new OutputStreamWriter(fos, "UTF-8");
			writer.write(data);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			try{
				if (writer != null) {
					writer.close();
				}
				if (fos != null)
					fos.close();
			}catch (Exception localException1){}
		}finally{
			try{
				if (writer != null) {
					writer.close();
				}
				if (fos != null)
					fos.close();
				}catch (Exception localException2){}
		}
	}

	public static String readFile(String filePath){
		StringBuffer buffer = new StringBuffer();
		try{
			File file = new File(filePath);
			FileInputStream fis = null;
			BufferedReader breader = null;
			try {
				fis = new FileInputStream(file);
				InputStreamReader isReader = new InputStreamReader(fis, "UTF-8");
				breader = new BufferedReader(isReader);
				String line;
				while ((line = breader.readLine()) != null){
					buffer.append(line);
					buffer.append("\r\n");
				}
				breader.close();
				isReader.close();
				fis.close();
			}catch (FileNotFoundException e) {
				logger.error(e.getMessage());
			} catch (IOException e) {
				logger.error(e.getMessage());
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return buffer.toString();
	}
}
